Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

stream-file-type

Package Overview
Dependencies
Maintainers
1
Versions
10
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

stream-file-type

Get the [file type](https://github.com/sindresorhus/file-type) by inspecting a stream.

  • 0.4.0
  • Source
  • npm
  • Socket score

Version published
Maintainers
1
Created
Source

Stream File Type

Get the file type by inspecting a stream.

Usage

const fs = require('fs')
const FileType = require('stream-file-type')

const input = fs.createReadStream('cat.jpg')
const detector = new FileType()

// Listen for event...
detector.on('file-type', (fileType) => {
  if (fileType === null) {
    console.log(`The mime type of "cat.jpg" could not be determined`)
  } else {
    console.log(`The file "cat.jpg" has the "${fileType.mime}" mime type`)
  }
})

// ...or get a Promise
detector.fileTypePromise().then((fileType) => {
  if (fileType === null) {
    console.log(`The mime type of "cat.jpg" could not be determined`)
  } else {
    console.log(`The file "cat.jpg" has the "${fileType.mime}" mime type`)
  }
})

input.pipe(detector).resume()

API

new FileType() => DuplexStream

Returns a new DuplexStream that will detect the file type of the content passing thru. All the data is passed as-is right thru the stream, and can be further piped to another destination.

When enough bytes have come thru to determine the file type (currently 4100) the event file-type will be emitted with the result of the detection. The result will either be null or an object with ext and mime.

FileType#fileTypePromise() => Promise

Returns a Promise of the detected file type. If the file-type event has already been emitted, the promise will be resolved with the result, otherwise the promise will be resolved when the file-type is detected, or rejected if an error occurs.

FAQs

Package last updated on 27 Oct 2019

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc